package com.peanut.baby.mvp.main.circle;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.Ad;
import com.peanut.baby.model.BBSMoment;
import com.peanut.baby.model.CircleTrends;
import com.peanut.baby.model.User;

import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/8/18.
 */

public class CircleFragmentPresenter implements CircleFragmentContract.Presenter{
    private CircleFragmentContract.View view;
    Activity    activity;
    public CircleFragmentPresenter(CircleFragmentContract.View view,Activity activity) {
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void getFamousUserList() {
        Observable obs = RetrofitClient.getInstance().getCircleFamousPerson(InitManager.getInstance().getUserId());
        obs.compose(BaseScheduler.<List<User>>compose()).compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<User>>() {
            @Override
            protected void onHandleSuccess(List<User> o) {
                view.onGetFamousList(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onRequestFailed(msg);
            }

        });
    }

    @Override
    public void getAllMoments(int pageNo, int pageSize) {
        Observable obs = RetrofitClient.getInstance().getAllMoments(pageNo,
                pageSize);
        obs.compose(BaseScheduler.<List<BBSMoment>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<BBSMoment>>() {
            @Override
            protected void onHandleSuccess(List<BBSMoment> o) {
                view.onGetAllMoments(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onRequestFailed(msg);
            }

        });
    }

    @Override
    public void getCircleMoments(int pageNo, int pageSize) {
        //if not login
        if(InitManager.getInstance().getUser() == null){
            view.onGetCircleMoments(null);
            return;
        }
        Observable obs = RetrofitClient.getInstance().getFollowingMoments(InitManager.getInstance().getUserId(),
                pageNo,
                pageSize,InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<List<BBSMoment>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<BBSMoment>>() {
            @Override
            protected void onHandleSuccess(List<BBSMoment> o) {
                view.onGetCircleMoments(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onRequestFailed(msg);
            }

        });
    }

    @Override
    public void changeFollowStatus(final User famous) {
        if(InitManager.getInstance().getUser() == null){
            view.onRequestFailed("操作失败，请先登录");
            return;
        }
        Observable obs = RetrofitClient.getInstance().changeFollowStatus(InitManager.getInstance().getUserId(),
                famous.userId+"",
                InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<User>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<User>() {
            @Override
            protected void onHandleSuccess(User o) {
                famous.isConcerned = o.isConcerned;
                view.onChangeFollowStatusSuccess(famous);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onRequestFailed(msg);
            }

        });
    }

    @Override
    public void getBanner() {
        Observable obs = RetrofitClient.getInstance().getBannerList(4);
        obs.compose(BaseScheduler.<BaseResponse<List<Ad>>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<Ad>>() {
                    @Override
                    protected void onHandleSuccess(List<Ad> ads) {
                        view.onBannerGet(true,ads,"");
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onBannerGet(false,null,msg);
                    }

                });
    }

    @Override
    public void getTrends() {
        Observable obs = RetrofitClient.getInstance().getCircleTrends(InitManager.getInstance().getUserId(),InitManager.getInstance().getUserToken());
        obs.compose(BaseScheduler.<BaseResponse<CircleTrends>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<CircleTrends>() {
                    @Override
                    protected void onHandleSuccess(CircleTrends trends) {
                        view.onTrendGets(true,trends,"");
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onTrendGets(false,null,msg);
                    }

                });
    }
}
